package com.derbysoft.synxisadapter.action.monitor.reservation;

import com.derbysoft.core.LimitedList;
import com.derbysoft.synxisadapter.action.DMXAbstractAction;
import com.derbysoft.synxisadapter.commons.exception.ReservationNotFoundException;
import com.derbysoft.synxisadapter.commons.model.Reservation;
import com.derbysoft.synxisadapter.commons.repository.ReservationRepository;
import com.derbysoft.synxisadapter.commons.repository.impl.ReservationCondition;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * User: jason
 * Date: 2013-02-21
 */
public class DMXAction extends DMXAbstractAction {

    @Autowired
    private ReservationRepository reservationRepository;

    private ReservationCondition condition;

    @Action("dmxQueryReservations")
    public String dmxQuery() {
        try {
            LimitedList<Reservation> limitedList = reservationRepository.findByCondition(condition, createLimit());
            return populateSucceededJsonResult(dmxPaging(limitedList, super.getPage(), limitedList.getTotalcount()), "reservation", "reservationLog");
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    @Action("dmxViewDetail")
    public String dmxReservationDetail() {
        try {
            if (condition == null || StringUtils.isBlank(condition.getErsp())) {
                return populateSucceededJsonResult("");
            }
            Reservation reservation = reservationRepository.findByErspCode(condition.getErsp());
            if (reservation == null) {
                throw new ReservationNotFoundException(condition.getErsp());
            }
            return populateSucceededJsonResult(reservation, "reservationLog");
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    public ReservationCondition getCondition() {
        return condition;
    }

    public void setCondition(ReservationCondition condition) {
        this.condition = condition;
    }
}
